const logger = require('../utils/log4js')
const { verifyToken } = require('../utils/jwt')
const wsObj = require('./wsObj')

let online = []

module.exports = (app) => {
	app.ws('/', (ws, req) => {
		logger.info('client connect!',req.body)
		logger.info('在线人数：', online.length)
		logger.info('在线用户：', online.map(it => it.userInfo.name))
		ws.on('message', (msg) => {
			logger.info('message', msg)
			const { url, params } = JSON.parse(msg)

			if (Object.keys(wsObj).some(key => key === url)) {
				wsObj[url](ws, params, online)
			} else {
				logger.info('this message not supported')
			}
		})

		ws.on('close', () => {
			console.log('websocket is close')
		})
	})
}